/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fractionapplication;

/**
 *
 * @author Ngocvan
 */
public class Fraction implements Comparable{
    private int numTor;
    private int denTor;
//    private boolean isIrreducible;

    public Fraction(){
        this(2010,1987);
    }
    public Fraction(int numTor, int denTor) {
        this.numTor = numTor;
        this.denTor = denTor;
//        this.isIrreducible = false;
    }
    
    public void simplify(){
//        find max divisor
        int maxDivisor = 1;
        int minVal = numTor;
        if (minVal > denTor) minVal = denTor;
        for (int i = minVal; i > 1; i--) {
            if ((numTor % i == 0) && ( denTor % i == 0)) {
                maxDivisor = i;
                break;
            }
        }
        denTor = denTor/maxDivisor;
        numTor = numTor/maxDivisor;
    }
    //for that it can be sorted
    @Override
    public int compareTo(Object t) {
        Fraction otherFrac = (Fraction) t;
        double d1 = (double) this.numTor/this.denTor;
        double d2 = (double) otherFrac.numTor/otherFrac.denTor;
        return (d1 < d2) ? -1 : (d1 > d2) ? 1 : 0;
    }
    
    @Override
    public String toString(){
        return "[" + numTor + "/" + denTor +"]";
        
    }
}
